package ar.edu.uba.fi.jmit.monitorengine.backend.client.view;

import java.util.List;

import ar.edu.uba.fi.jmit.monitorengine.backend.client.controller.ReceivedMessagesController;
import ar.edu.uba.fi.jmit.monitorengine.backend.shared.dto.EventDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
/**
 * Importante, agregar paginado.
 * @author Tomas
 *
 */
public class RecivedEventsPanel extends VerticalPanel implements ValueChangeHandler<String> {
	
	private static final String PAGINATION_PREFIX = "pagination-";
	
	private Grid eventGrid;
	
	private Panel pagination;
	
	private ReceivedMessagesController controller;

	public RecivedEventsPanel() {
		super();
		this.setSize("800px", "100%");
		this.add(new HTML("<h1>Last Received Events</h1>"));
		eventGrid = new Grid(0, 4);
		eventGrid.setTitle("Last Received Events");
		pagination = new HorizontalPanel();
		this.add(eventGrid);
		this.add(pagination);
		History.addValueChangeHandler(this);
	}
	
	@Override
	protected void onLoad() {
		GWT.log("Loading recived messages panel");
		super.onLoad();
	}

	public void showEvents(List<EventDTO> events) {
		GWT.log("on showEvents(List<EventDTO> events), showing " + events.size() + " events.");
		eventGrid.clear();
		eventGrid.resizeRows(events.size() + 1);
		eventGrid.setBorderWidth(2);
		eventGrid.setSize("100%", "100%");
		int i = 1;
		this.addHeader(eventGrid);
		for(EventDTO event:events) {
			this.addRow(i, eventGrid, event);
			i++;
		}
	}

	private void addHeader(Grid grid) {
		GWT.log("Adding Header");
		grid.setText(0, 0, "ID");
		grid.setText(0, 1, "Event Date");
		grid.setText(0, 2, "Priority");
		grid.setText(0, 3, "Source");
		
	}

	private void addRow(int rowNum, Grid grid, EventDTO event) {
		grid.setText(rowNum, 0, event.getId().toString());
		grid.setText(rowNum, 1, event.getEventDate().toString());//TODO format
		grid.setText(rowNum, 2, event.getPriority());
		grid.setText(rowNum, 3, event.getSource());
		
	}

	public void showPagination(int pageCount, int showingPage) {
		GWT.log("Showing Pagination. PageCount: " + pageCount + " Showing Page: " + showingPage);
		pagination.clear();
		pagination.add(createPageLink("first", "|<", false));
		pagination.add(getPageLinkSeparator());
		pagination.add(createPageLink("previous", "<", false));
		pagination.add(getPageLinkSeparator());
		for(int i = 0; i < pageCount; i++) {
			pagination.add(createPageLink(String.valueOf(i), String.valueOf(i), i==showingPage));
			pagination.add(getPageLinkSeparator());
		}
		pagination.add(createPageLink("next", ">", false));
		pagination.add(getPageLinkSeparator());
		pagination.add(createPageLink("last", ">|", false));
		
	}

	private Widget getPageLinkSeparator() {
		return new HTML("&nbsp;");
	}

	private Widget createPageLink(String string, String text, boolean actualPage) {
		if(actualPage) {
			text = "<b>" + text + "</b>";
		}
		return new Hyperlink(text, true, PAGINATION_PREFIX + string);
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> value) {
		if(isPagination(value)) {
			String paginationValue = getPaginationValue(value.getValue());
			if(paginationValue.equals("first")) {
				controller.showFirstPage();
			}else if(paginationValue.equals("previous")) {
				controller.showPreviosPage();
			}else if(paginationValue.equals("next")) {
				controller.showNextPage();
			}else if(paginationValue.equals("last")) {
				controller.showLastPage();
			}else {
				Integer pageNumber = Integer.valueOf(paginationValue);
				controller.showRecivedMessages(pageNumber);
			}
			History.back();
		}
	}

	private String getPaginationValue(String value) {
		return value.substring(PAGINATION_PREFIX.length());
	}

	private boolean isPagination(ValueChangeEvent<String> value) {
		return value.getValue().startsWith(PAGINATION_PREFIX);
	}

	public void setController(
			ReceivedMessagesController receivedMessagesController) {
		this.controller = receivedMessagesController;
		
	}
	
	
}
